package com.bistu.openlab.uofspreadsheet.xmlparse;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.bistu.openlab.uofspreadsheet.model.meta.ExcelMeta;
import com.bistu.openlab.uofspreadsheet.namespace.NamespaceDeal;
import com.bistu.openlab.uofspreadsheet.util.TypeConvertor;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-8 
 */
public class ExcelMetaParse implements IXmlParse<ExcelMeta> {
	private static ExcelMetaParse parse = new ExcelMetaParse();
	
	private ExcelMetaParse(){
		
	}
	/**
	 * singleton method
	 * @return
	 */
	public static ExcelMetaParse getInstance(){
		return parse;
	}

	@Override
	public ExcelMeta readFromElement(Element root) {
		ExcelMeta meta = new ExcelMeta();
		//create a xml document
		Element doc = root;
		if(doc!=null){
			//creator
			Element eleCreator = (Element)doc.selectSingleNode("元:元数据_5200/元:创建者_5203");
			if(eleCreator!=null)
				meta.setCreator(eleCreator.getText());
			//author
			Element eleAuthor = (Element)doc.selectSingleNode("元:元数据_5200/元:作者_5204");
			if(eleAuthor != null)
				meta.setAuthor(eleCreator.getText());
			//last editor
			Element eleLastEditor = (Element)doc.selectSingleNode("元:元数据_5200/元:最后作者_5205");
			if(eleLastEditor != null)
				meta.setLastEditor(eleLastEditor.getText());
			//create date
			Element eleCreateDate = (Element)doc.selectSingleNode("元:元数据_5200/元:创建日期_5207");
			if(eleCreateDate != null)
				meta.setCreateDate(TypeConvertor.GetDate(eleCreateDate.getText()));
			//edit time
			Element eleEditTime = (Element)doc.selectSingleNode("元:元数据_5200/元:编辑时间_5209");
			if(eleEditTime != null)
				meta.setEditTime(TypeConvertor.GetInt(eleEditTime.getText()));
			//create application
			Element eleCreateApp = (Element)doc.selectSingleNode("元:元数据_5200/元:创建应用程序_520A");
			if(eleCreateApp != null)
				meta.setCreateApp(eleCreateApp.getText());
			//corporation name
			Element eleCorpName = (Element)doc.selectSingleNode("元:元数据_5200/元:公司名称_5213");
			if(eleCorpName != null)
				meta.setCorpName(eleCorpName.getText());
		}
		
		return meta;
	}
	@Override
	public Element writeToElement(ExcelMeta meta) {
		Element root = DocumentHelper.createElement("元:元数据_5200");
		NamespaceDeal.MetaNamespaceAdd(root);
		//creator
		Element eleCreator = root.addElement("元:创建者_5203");
		if(eleCreator!=null)
			eleCreator.setText(meta.getCreator());
		//author
		Element eleAuthor = root.addElement("元:作者_5204");
		if(eleAuthor != null)
			eleAuthor.setText(meta.getAuthor());
		//last editor
		Element eleLastEditor = root.addElement("元:最后作者_5205");
		if(eleLastEditor != null)
			eleLastEditor.setText(meta.getLastEditor());
		//create date
		Element eleCreateDate = root.addElement("元:创建日期_5207");
		if(eleCreateDate != null)
			eleCreateDate.setText(TypeConvertor.GetString(meta.getCreateDate()));
		//edit time
		Element eleEditTime = root.addElement("元:编辑时间_5209");
		if(eleEditTime != null)
			eleEditTime.setText(meta.getEditTime()+"");
		//create application
		Element eleCreateApp = root.addElement("元:创建应用程序_520A");
		if(eleCreateApp != null)
			eleCreateApp.setText(meta.getCreateApp());
		//corporation name
		Element eleCorpName = root.addElement("元:公司名称_5213");
		if(eleCorpName != null)
			eleCorpName.setText(meta.getCorpName());
		return root;
	}
}
